//
//  CarTypeViewController.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/10/18.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "CarTypeViewController.h"
#import "UIColor+Hex.h"
#import "MBProgressHUD+MJ.h"
#import "AddCarViewController.h"
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height//获取屏幕高度，兼容性测试
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width//获取屏幕宽度，兼容性测试
@interface CarTypeViewController ()

@property NSArray *type1;
@property NSArray *type2;
@property NSArray *type3;

@property NSArray *cartype;
@property NSArray *doorType;

@property NSMutableArray *contents;
@property NSMutableArray *typeBtns;
@property NSMutableArray *doorBtns;

@property NSInteger pos1;
@property NSInteger pos2;
@property NSInteger pos3;
@end

@implementation CarTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _contents = [[NSMutableArray alloc] init];
    _typeBtns = [[NSMutableArray alloc] init];
    _doorBtns = [[NSMutableArray alloc] init];
    
    // Do any additional setup after loading the view.
    _contentView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [self initData];
    [self initView];
}

-(void)initData{
    _type1 = @[@"面包车",@"2.5米",@"3.8-4.2米",@"5.2-6.2米",@"6.8-7.2米",@"8.6-9.6米"];
    _type2 = @[@"核载0.5-1吨",@"核载0.5吨",@"核载1.9吨",@"核载5吨",@"核载10吨",@"核载10-15吨"];
    _type3 = @[@"2.5-4.5m³",@"5m³",@"15m³",@"25m³",@"35m³",@"45-50m³"];
    
    _cartype = @[@"厢式货车",@"高栏货车",@"低栏货车",@"任意车型"];
    _doorType = @[@"开边",@"开顶",@"边门",@"尾板",@"防雨",];
    
    _pos1 = -1;
    _pos2 = -1;
    _pos3 = -1;
}


-(void)initView{
    int height = 0;
    for (int i = 0; i < _type1.count; i++) {
        UIButton * control = [[UIButton alloc] initWithFrame:CGRectMake(0, height, ScreenWidth, 50)];
        [control setBackgroundImage:[self createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [control setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#f7f7f7"]] forState:UIControlStateSelected];
        control.tag = i;
        [control addTarget:self action:@selector(btn_click:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *lab1 = [[UILabel alloc] initWithFrame:CGRectMake(20, height, ScreenWidth / 2 - 20, 50)];
        lab1.text = _type1[i];
        lab1.font = [UIFont systemFontOfSize:13];
        
        UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2, height, ScreenWidth / 2, 25)];
        lab2.text = _type2[i];
        lab2.font = [UIFont systemFontOfSize:13];
        lab2.textColor = [UIColor colorWithHexString:@"#cccccc"];
        lab2.contentMode = UIViewContentModeBottom;
        UILabel *lab3 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2, height + 25 , ScreenWidth / 2, 25)];
        lab3.text = _type3[i];
        lab3.font = [UIFont systemFontOfSize:13];
        lab3.textColor = [UIColor colorWithHexString:@"#cccccc"];
        lab3.contentMode = UIViewContentModeTop;

        height = height + 50;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, height, ScreenWidth, 1)];
        line.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
        height = height + 1;
        
        [_contentView addSubview:control];
        [_contentView addSubview:lab1];
        [_contentView addSubview:lab2];
        [_contentView addSubview:lab3];
        [_contentView addSubview:line];
        
        [_contents addObject:control];
    }
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, height, ScreenWidth, 3)];
    line.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    [_contentView addSubview:line];
    
    height = height + 3;
    
    UIView *bgView1 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 50)];
    bgView1.backgroundColor = [UIColor whiteColor];
    [_contentView addSubview:bgView1];
    for (int i = 0; i < _cartype.count; i++) {
        int widget = ( ScreenWidth - 100 ) / 4;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20 * ( i + 1 ) + i * widget, height + 10, widget, 30)];
        btn.tag = i;
        [btn setTitle:_cartype[i] forState:UIControlStateNormal];
        [btn setTitle:_cartype[i] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor colorWithHexString:@"#4c4c4c"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[self createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateSelected];
        [btn.layer setBorderWidth:1];
        [btn.layer setBorderColor:[UIColor colorWithHexString:@"#4c4c4c"].CGColor];
         btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn addTarget:self action:@selector(btn_click2:) forControlEvents:UIControlEventTouchUpInside];
        [_typeBtns addObject:btn];
        [_contentView addSubview:btn];
    }
    height = height + 50;
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, height, ScreenWidth, 3)];
    line1.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    [_contentView addSubview:line1];
    
    height = height + 3;
    //边门等
    
    UIView *bgView2 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 50)];
    bgView2.backgroundColor = [UIColor whiteColor];
    [_contentView addSubview:bgView2];
    int widget = ( ScreenWidth - 100 ) / 4;
    for (int i = 0; i < 4; i++) {
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20 * ( i + 1 )+ i * widget, height + 10, widget, 30)];
        btn.tag = i;
        [btn setTitle:_doorType[i] forState:UIControlStateNormal];
        [btn setTitle:_doorType[i] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor colorWithHexString:@"#4c4c4c"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[self createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateSelected];
        [btn.layer setBorderWidth:1];
        [btn.layer setBorderColor:[UIColor colorWithHexString:@"#4c4c4c"].CGColor];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn addTarget:self action:@selector(btn_click3:) forControlEvents:UIControlEventTouchUpInside];
        
        [_doorBtns addObject:btn];
        [_contentView addSubview:btn];
    }
    height = height + 50;
    
    UIView *bgView3 = [[UIView alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 50)];
    bgView3.backgroundColor = [UIColor whiteColor];
    [_contentView addSubview:bgView3];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20 , height + 10, widget, 30)];
    btn.tag = 4;
    [btn setTitle:_doorType[4] forState:UIControlStateNormal];
    [btn setTitle:_doorType[4] forState:UIControlStateSelected];
    [btn setTitleColor:[UIColor colorWithHexString:@"#4c4c4c"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [btn setBackgroundImage:[self createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [btn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateSelected];
    [btn.layer setBorderWidth:1];
    [btn.layer setBorderColor:[UIColor colorWithHexString:@"#4c4c4c"].CGColor];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn addTarget:self action:@selector(btn_click3:) forControlEvents:UIControlEventTouchUpInside];
    
    [_doorBtns addObject:btn];
    [_contentView addSubview:btn];
    
    height = height + 50;

     height = height + 20;
    //提交
    UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, height, ScreenWidth - 20, 50)];
    nextBtn.tag = 105;
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [nextBtn setTitle:@"完成" forState:UIControlStateNormal];
    [nextBtn setTitle:@"完成" forState:UIControlStateSelected];
    [nextBtn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateNormal];
    nextBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [nextBtn addTarget:self action:@selector(limit_click) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:nextBtn];
    
    _contentView.contentSize = CGSizeMake(ScreenWidth, height + 50);
}
-(void)limit_click{
    //提交
    if(_pos1 < 0 || _pos2 < 0 || _pos3 < 0){
        [MBProgressHUD showError:@"请选择车辆类型"];
        return;
    }
    NSMutableString *data =[[NSMutableString alloc]init];
    [data appendFormat:@"%@,%@,%@,%@,%@",_cartype[_pos2],_doorType[_pos3],_type1[_pos1],_type2[_pos1],_type3[_pos1]];
    

    AddCarViewController *vc = self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2];
    vc.type = data;
    
    [self.navigationController popToViewController:vc animated:YES];
    
}
-(void)btn_click:(UIButton *)btn{
    int tag = btn.tag;
    if (_pos1 < 0) {
        btn.selected = YES;
        [btn.layer setBorderColor:[UIColor colorWithHexString:@"#ff7e00"].CGColor];
        
        _pos1 = tag;
    }else if (_pos1 != tag){
        btn.selected = YES;
        [btn.layer setBorderColor:[UIColor colorWithHexString:@"#ff7e00"].CGColor];
        
        UIButton *oldBtn = _contents[_pos1];
        oldBtn.selected = NO;
        [oldBtn.layer setBorderColor:[UIColor colorWithHexString:@"#4c4c4c"].CGColor];
        _pos1 = tag;
    }
}

-(void)btn_click2:(UIButton *)btn{
    int tag = btn.tag;
    if (_pos2 < 0) {
        btn.selected = YES;
        [btn.layer setBorderColor:[UIColor colorWithHexString:@"#ff7e00"].CGColor];

        _pos2 = tag;
    }else if (_pos2 != tag){
        btn.selected = YES;
        [btn.layer setBorderColor:[UIColor colorWithHexString:@"#ff7e00"].CGColor];
        
        UIButton *oldBtn = _typeBtns[_pos2];
        oldBtn.selected = NO;
        [oldBtn.layer setBorderColor:[UIColor colorWithHexString:@"#4c4c4c"].CGColor];
        _pos2 = tag;
    }
}
-(void)btn_click3:(UIButton *)btn{
    int tag = btn.tag;
    if (_pos3 < 0) {
        btn.selected = YES;
        [btn.layer setBorderColor:[UIColor colorWithHexString:@"#ff7e00"].CGColor];
        
        _pos3 = tag;
    }else if (_pos3 != tag){
        btn.selected = YES;
        [btn.layer setBorderColor:[UIColor colorWithHexString:@"#ff7e00"].CGColor];
        
        UIButton *oldBtn = _doorBtns[_pos3];
        oldBtn.selected = NO;
        [oldBtn.layer setBorderColor:[UIColor colorWithHexString:@"#4c4c4c"].CGColor];
        _pos3 = tag;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}
- (IBAction)back_click:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
